
//获取应用实例
const app = getApp()

Page({
  data: {
    abroadCountry: null,
    abroadDateStr: '',
    userId: null,
    id: null,
    sreturnDateStr: '',
    areturnDateStr: '',
    abroadSettleDateStr: '',
    abroadReason: null,
    abroadSituation: null,
    abroadTask: null,
    remark: null,
    action: null,
    wh:0
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    if (app.globalData.userId) {
      this.setData({
        userId: app.globalData.userId
      })
    }
    var action = options.action;
    this.setData({
      action: action
    })
    if (action != 'add') {
      var id = options.id;
      var abroadCountry = options.abroadCountry;
      var abroadDateStr = options.abroadDateStr;
      var sreturnDateStr = options.sreturnDateStr;
      var areturnDateStr = options.areturnDateStr;
      var abroadSettleDateStr = options.abroadSettleDateStr;
      var abroadReason = options.abroadReason;
      var abroadSituation = options.abroadSituation;
      var abroadTask = options.abroadTask;
      var remark = options.remark;
      this.setData({
        id: id,
        abroadCountry: abroadCountry,
        abroadDateStr: abroadDateStr,
        sreturnDateStr: sreturnDateStr,
        areturnDateStr: areturnDateStr,
        abroadSettleDateStr: abroadSettleDateStr,
        abroadReason: abroadReason,
        abroadSituation: abroadSituation,
        abroadTask: abroadTask,
        remark: remark
      })
    }
  },
  abroadCountryChange(e) {
    var abroadCountry = e.detail.value;
    this.setData({
      abroadCountry: abroadCountry
    })
  },
  abroadDateChange(e) {
    this.setData({
      abroadDateStr: e.detail.value
    })
  },
  sreturnDateChange(e) {
    this.setData({
      sreturnDateStr: e.detail.value
    })
  },
  areturnDateChange(e) {
    this.setData({
      areturnDateStr: e.detail.value
    })
  },
  abroadSettleDateChange(e) {
    this.setData({
      abroadSettleDateStr: e.detail.value
    })
  },
  reasonChange(e) {
    this.setData({
      abroadReason: e.detail.value
    })
  },
  abroadSituationChange(e) {
    this.setData({
      abroadSituation: e.detail.value
    })
  },
  abroadTaskChange(e) {
    this.setData({
      abroadTask: e.detail.value
    })
  },
  remarkChange(e) {
    var remark = e.detail.value;
    this.setData({
      remark: remark
    })
  },
  onShow(){
  var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  submit(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 5000
    })
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/abroad/save',
      data: {
        id: that.data.id,
        abroadCountry: that.data.abroadCountry,
        abroadDate: that.data.abroadDateStr,
        sreturnDate: that.data.sreturnDateStr,
        areturnDate: that.data.areturnDateStr,
        abroadSettleDate: that.data.abroadSettleDateStr,
        abroadReason: that.data.abroadReason,
        abroadSituation: that.data.abroadSituation,
        abroadTask: that.data.abroadTask,
        remark: that.data.remark,
        userId: that.data.userId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideToast();
        if (res.data.syscode == 0) {
          wx.showToast({
            title: '提交成功',
            duration: 1000
          })
          wx.navigateBack({
            delta: 1
          });
        }
        else {
          wx.showToast({
            title: '操作失败',
            duration: 3000
          })
        }
      },
      fail(res) {
        wx.hideToast();
        wx.showToast({
          title: '操作失败，请检查网络',
          duration: 3000
        })
      }
    })
  }
});